package sdfi.lee.DAO;


import java.io.File;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import sdfi.lee.Entity.Notice;
import sdfi.lee.Entity.Noticefile;

public class NoticeDAO {
	public String addNotice(Notice notice)
	{
		Session session=DAO.openSession();
		Transaction ts=null;
		try
		{
			
			ts=session.beginTransaction();
			if(null!=notice.getFiles()&&notice.getFiles().length>0)
			{	
			
				for(String filename : notice.getFilesFileName())
				{
					Noticefile noticeFile=new Noticefile();
					noticeFile.setNoticepath(filename);
					noticeFile.setNoticeid(notice);
					notice.addTonoticefiles(noticeFile);
				}
			}
			session.save(notice);
			ts.commit();
			return "success";
		}
		catch(Exception ee)
		{
			System.out.println("Exception ee when add Notice");
			ts.rollback();
			ee.printStackTrace();
			return "error";
		}
		finally
		{
			DAO.closeSession();
		}
		
	}
	public List<Notice> getAllNotice(int page,int size)
	{
		List<Notice> list=null;
		Session session=null;
		Transaction ts=null;
		try
		{
			session=DAO.openSession();
			ts=session.beginTransaction();
			Query query=session.createQuery("from Notice Order by noticedate desc");
			query.setFirstResult((page-1)*size);
			query.setMaxResults(size);
			list=query.list();
			ts.commit();
			return list;
		}
		catch(Exception ee)
		{
			ts.rollback();
			System.out.println("Exception ee when get Notice");
			ee.printStackTrace();
			return list;
			
		}
		finally
		{
			DAO.closeSession();
		}
	}
	public Notice getNoticeById(Integer id)
	{
		Notice notice=null;
		Session session=DAO.openSession();
		Transaction ts=null;
		try
		{
			ts=session.beginTransaction();
			notice=(Notice)session.load(Notice.class, id);
			ts.commit();
		}
		catch(Exception ee)
		{
			ts.rollback();
			System.out.println("Exception ee when get notice by id");
			ee.printStackTrace();
		}
		return notice;
	}
	public String deleteNotice(String[] noticeids,String path)
	{
		Session session=null;
		Transaction ts=null;
		try
		{
			session=DAO.openSession();
			ts=session.beginTransaction();
			if(null!=noticeids&&noticeids.length>0)
			{
				for(String target:noticeids)
				{
					Integer id=Integer.valueOf(target);
					Notice notice=(Notice)session.load(Notice.class, id);
					for(Iterator<Noticefile> it=notice.getNoticefiles().iterator();it.hasNext();)
					{
						File file=new File(path,it.next().getNoticepath());
						this.deleteFile(file);
					}
					session.delete(notice);
				}
			}
			ts.commit();
		}
		catch(Exception ee)
		{
			System.out.println("Exception ee when delete notice");
			ee.printStackTrace();
		}
		finally
		{
			DAO.closeSession();
		}
		return "success";
	}
	public String modifyNotice(Notice notice,String path)
	{
		Session session=DAO.openSession();
		Transaction ts=null;
		try
		{
			ts=session.beginTransaction();
			Notice old=(Notice)session.get(Notice.class, notice.getId());
			if(notice.getFiles()!=null&&notice.getFiles().length>0)
			{
				if(old.getNoticefiles()!=null&&old.getNoticefiles().size()>0)
				{
					Iterator<Noticefile> it=old.getNoticefiles().iterator();
					while(it.hasNext())
					{
						Noticefile nfile=it.next();
						File f=new File(path,nfile.getNoticepath());
						this.deleteFile(f);
						nfile.setNoticeid(null);
						old.getNoticefiles().clear();
						session.delete(nfile);
					}
					//�����ݿ�
					
				}
				for(String na:notice.getFilesFileName())
				{
					Noticefile nf=new Noticefile();
					nf.setNoticepath(na);
					nf.setNoticeid(old);
					old.addTonoticefiles(nf);
				}
				}
				session.update(old);
				
				ts.commit();
			return "success";
		}
		catch(Exception ee)
		{
			ee.printStackTrace();
			ts.rollback();
			return "error";
		}
		
	}
	private void deleteFile(File file)
	{
		if(file.exists())
		{
			try
			{
			file.delete();
			}
			catch(Exception ee)
			{
				ee.printStackTrace();
			}
		}
	}
	

}
